from sqlmodel import Session, select
import db.models as models


def check_pwd(db: Session, uname: str, pwd: str):
    stmt = select(models.User).where(models.User.uname == uname)
    whoami = db.exec(stmt).first()
    if whoami is not None and whoami.pwd == pwd:
        return (whoami.id, whoami.role)
    return None


def update_pwd(db: Session, uname: str, pwd: str):
    stmt = select(models.User).where(models.User.uname == uname)
    whoami = db.exec(stmt).first()
    whoami.pwd = pwd
    db.commit()
    return True


def check_user_exists(db: Session, uname: str):
    stmt = select(models.User).where(models.User.uname == uname)
    return db.exec(stmt).first() is not None


def create_user(db: Session, uname: str, pwd: str, role: int = 1):
    user = models.User(uname=uname, pwd=pwd, role=role)
    db.add(user)
    db.commit()
    db.refresh(user)
    return user


def create_wx_user(
    db: Session,
    uname: str,
    pwd: str,
    role: int = 1,
    openid: str = None,
    nickname: str = None,
    phone: str = None,
    headimgurl: str = None,
):
    user = models.User(
        uname=uname,
        pwd=pwd,
        role=role,
        openid=openid,
        nickname=nickname,
        phone=phone,
        headimgurl=headimgurl,
    )
    db.add(user)
    db.commit()
    db.refresh(user)
    return user


def query_all_user(db: Session):
    stmt = select(models.User)
    all_users = db.exec(stmt).all()
    all_list = [
        {"name": user_i.uname, "role": "管理员" if user_i.role == 0 else "普通用户"}
        for user_i in all_users
    ]
    return all_list


##############Message#########


def create_message(db: Session, uid: str, msg_title: str, msg_content: str):
    msg = models.Message(user_id=uid, msg_title=msg_title, msg_content=msg_content)
    db.add(msg)
    db.commit()
    db.refresh(msg)
    return msg


def update_message(db: Session, id: str, msg_title: str, msg_content: str):
    stmt = select(models.Message).where(models.Message.id == id)
    msg = db.exec(stmt).first()
    msg.msg_title = msg_title
    msg.msg_content = msg_content
    db.commit()
    db.refresh(msg)
    return msg


def query_all_message(db: Session):
    stmt = select(
        models.Message.msg_title, models.Message.msg_content, models.User.uname
    ).join(models.User)
    return db.exec(stmt).all()


def query_all_message_by_uid(db: Session, uid: str):
    stmt = (
        select(
            models.Message.id,
            models.Message.msg_title,
            models.Message.msg_content,
            models.User.uname,
        )
        .join(models.User)
        .where(models.User.id == uid)
    )
    return db.exec(stmt).all()


def query_message_by_id(db: Session, id: str):
    stmt = select(models.Message.msg_title, models.Message.msg_content).where(
        models.Message.id == id
    )
    return db.exec(stmt).first()


##############Log#########
def create_login_log(db: Session, uname: str, log_content: str, log_level: int):
    logobj = models.Log(
        uname=uname, log_type=0, log_content=log_content, log_level=log_level
    )
    db.add(logobj)
    db.commit()
    db.refresh(logobj)
    return logobj


def create_log(db: Session, uid: str, log_type: int, log_content: str, log_level: int):
    logobj = models.Log(
        user_id=uid, log_type=log_type, log_content=log_content, log_level=log_level
    )
    db.add(logobj)
    db.commit()
    db.refresh(logobj)
    return logobj


def query_all_log(db: Session):
    stmt = select(models.Log)
    all_logs = db.exec(stmt).all()
    log_level_dict = {0: "info", 1: "debug", 2: "warning", 3: "error"}
    log_type_dict = {
        0: "登录日志",
        1: "操作日志",
    }
    all_list = [
        {
            "id": log_i.id,
            "log_level": log_level_dict[log_i.log_level],
            "log_level_org": log_i.log_level,
            "uname": log_i.uname,
            "log_type": log_type_dict[log_i.log_type],
            "log_content": log_i.log_content,
            "log_time": log_i.create_datetime,
        }
        for log_i in all_logs
    ]
    return all_list
